package com.chuangke.common.express.function.grid;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.chuangke.common.express.function.string.StringAppendFunction;
import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.runtime.function.AbstractFunction;
import com.googlecode.aviator.runtime.type.AviatorJavaType;
import com.googlecode.aviator.runtime.type.AviatorObject;
import com.googlecode.aviator.runtime.type.AviatorString;

public class LNFunction extends AbstractFunction {
	
	
    private static final long serialVersionUID = -8456783404745666618L;
	
	public static final String ROW_NUM_KEY = "rowNum" ;

	public static void main(String[] args) {
		AviatorEvaluator.addFunction(new LNFunction());
		AviatorEvaluator.addFunction(new StringAppendFunction());

		Map<String, Object> env = new HashMap<>();
		env.put("rowNum", 1);

		String expression = "BPC(T_ACCOUNT=LN ( A ),T_FLOW='F_NONE',T_FLOW1=LN ( B ))";

//		Object value = CkEvaluatorExecutor.execute(expression, env);
//		System.out.print(value);

		Pattern p = Pattern.compile("(LN\\s*\\(\\s*\\w+\\s*\\))");
		Matcher m = p.matcher(expression);
		
		String newExpression = expression ;
		if (m.find()) {
//			System.out.println("开始索引：" + m.start()); // 开始索引：0
//			System.out.println("group():" + m.group()); // group():ab%12
			
			String gs = m.group();
			newExpression = newExpression.replace(gs, "A2");
			System.out.println(newExpression);
		}
		if (m.find()) {
//			System.out.println("开始索引：" + m.start()); // 开始索引：0
//			System.out.println("group():" + m.group()); // group():ab%12
			
			String newValue = "B2" ;
			String gs = m.group();
			newExpression = newExpression.replace(gs, newValue);
			System.out.println(newExpression);
		}
	}

	@Override
	public AviatorObject call(Map<String, Object> env, AviatorObject a1) {
		String column = ((AviatorJavaType) a1).getName();
		String rowNum = env.get(ROW_NUM_KEY) + "";
		return new AviatorString((column + rowNum).toLowerCase());
	}

	@Override
	public String getName() {
		return "LN";
	}

}
